//
//  AppDelegate+HE.m
//  huien
//
//  Created by yiban on 2017/5/26.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "AppDelegate+HE.h"
#import "HEGuidePageVC.h"

#import "HEGestureViewController.h"
#import "UserLoginController.h"
@interface AppDelegate ()<LcbLoginDelegate>

@end

@implementation AppDelegate (HE)


//引导页
-(void)enterGuidePage
{
    //判断是否需要显示：（内部已经考虑版本及本地版本缓存）
    BOOL canShow = [HEGuidePageVC canShowNewFeature];
    if(canShow){ // 初始化新特性界面
        self.window.rootViewController = [HEGuidePageVC newFeatureVCWithImageNames:@[@"he01",@"he02"] enterBlock:^{
            [LcbLog log:@"进入主页面"];
            [self enterMainPage];
        } configuration:^(UIButton *enterButton) { // 配置进入按钮
            [enterButton setBackgroundImage:[UIImage imageNamed:@"he"] forState:UIControlStateNormal];
            [enterButton setBackgroundImage:[UIImage imageNamed:@"he"] forState:UIControlStateHighlighted];
            enterButton.bounds = CGRectMake(0, 0, 200, 39);
            enterButton.center = CGPointMake([UIScreen mainScreen].bounds.size.width * 0.5, [UIScreen mainScreen].bounds.size.height* 0.9);
        }];
        
    }else{
        [LcbLog log:@"进入主页面"];
        [self enterMainPage];
    }
    
}
-(void)getsureLogin
{
    if ([HEShare isGestureOpen]) {
        if ([HEShare isPatternOpen]) {
            UserLoginController *userlogin =[UserLoginController newWithGestureDelegate:self];
            [self.window.rootViewController presentViewController:userlogin animated:YES completion:nil];
        }else if ([HEShare isFingerOpen])
        {
            [HEShare fingerCert];
        }
        
    }else
    {
        [self autoLogin];
    }
}
-(void)autoLogin
{
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:[[LcbCache sharedCache] objectForKey:@"userName"] forKey:@"userName"];
    [dict setValue:[[LcbCache sharedCache] objectForKey:@"password"] forKey:@"password"];
    [HEApi loginWithParam:dict serviceblock:^(id response, NSString *errorMsg) {
        if (response) {
            if ([response isKindOfClass:[SessionModel class]]) {
                SessionModel *sessionModel = (SessionModel*)response;
                [[JGShare sharedJGShare] setPushAlias:[NSString stringWithFormat:@"%ld",sessionModel.session_user.userId]];
                [[LcbCache sharedCache] setObject:sessionModel forKey:@"SessionModel"];
            }
        }
        
    }];
}
- (void)userLoginOK:(id)userinfo
{
    [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
    if ([userinfo isKindOfClass:[SessionModel class]]) {
        SessionModel *sessionModel = (SessionModel*)userinfo;
        [[JGShare sharedJGShare] setPushAlias:[NSString stringWithFormat:@"%ld",sessionModel.session_user.userId]];
        [[LcbCache sharedCache] setObject:sessionModel forKey:@"SessionModel"];
    }
}
- (void)userLoginFail
{
     [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
    AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate setSelectTable:0];
}
- (void)userDidCancel
{
    [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
    AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate setSelectTable:0];
}


@end
